package com.aistar.model.rbm;

import com.aistar.model.entity.Interview;
import com.aistar.service.employer.IInterviewService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * @ClassName: SendEmailRbm
 * @author: LiuWenXiu
 * @date: 2021/10/21 20:59
 * @Description: TODO...本类用来演示: 发送邮件
 */
@Component
public class SendEmailRbm {

    @Autowired
    private IInterviewService interviewService;


    // 监听到队列中有数据就发送面试邀约信息到邮箱
    @RabbitListener(queues = "hire_send")
    @RabbitHandler
    public void sendEmail(Interview interview, @Headers Map<String,Object> header, Channel channel){
//        System.out.println("收到的邀约信息："+interview);
////        System.out.println("我要发送简历啦");
////        System.out.println("监听到的简历id："+interview.getResumeId());
////        System.out.println(interview);
        // 调用发送简历的方法
        interviewService.sendToEmail(interview);

        //手动删除rabbitmq队列中的数据的.
        long ack = (long) header.get(AmqpHeaders.DELIVERY_TAG);
        try {
            channel.basicAck(ack,false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 监听到队列中有数据就插入数据到邀约面试表中
    @RabbitListener(queues = "hire_ins")
    @RabbitHandler
    public void insertToInterview(Interview interview, @Headers Map<String,Object> header, Channel channel){
//        System.out.println("我要插入数据啦");
//        System.out.println(interview);
        // 调用插入的方法
        interviewService.insertToInterview(interview);
        //手动删除rabbitmq队列中的数据的.
        long ack = (long) header.get(AmqpHeaders.DELIVERY_TAG);
        try {
            channel.basicAck(ack,false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RabbitListener(queues = "hire_update")
    @RabbitHandler
    public void updateDelivery(Integer deliveryId, @Headers Map<String,Object> header, Channel channel){
        // 调用插入的方法
        interviewService.updateDelivery(deliveryId);
        //手动删除rabbitmq队列中的数据的.
        long ack = (long) header.get(AmqpHeaders.DELIVERY_TAG);
        try {
            channel.basicAck(ack,false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @RabbitListener(queues = "hire_withdraw")
    @RabbitHandler
    public void withDraw(Integer interviewId, @Headers Map<String,Object> header, Channel channel){
        // 执行撤回功能
        interviewService.withDrawInterview(interviewId);
        //手动删除rabbitmq队列中的数据的.
        long ack = (long) header.get(AmqpHeaders.DELIVERY_TAG);
        try {
            channel.basicAck(ack,false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RabbitListener(queues = "hire_update_interview")
    @RabbitHandler
    public void updateInterview(Interview interview, @Headers Map<String,Object> header, Channel channel){
        // 执行撤回功能
//        System.out.println("执行撤回");
//        Interview interview = interviewService.getById(interviewId);
        interviewService.updateInterview(interview);
        //手动删除rabbitmq队列中的数据的.
        long ack = (long) header.get(AmqpHeaders.DELIVERY_TAG);
        try {
            channel.basicAck(ack,false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
